{include file='public/header' /}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini full-table">
            <div class="layui-form toolbar">
                <div class="layui-form-item">

                    <div class="layui-input-inline">
                        <select name="" lay-verify="required" id="">
                            <option value="0">请选择优惠券类型</option>
                            <option value="0">满减券</option>
                            <option value="0">无门槛券</option>
                        </select>
                    </div>
                    <div class="layui-input-inline">
                        <select name="" lay-verify="required" id="">
                            <option value="0">请选择状态</option>
                            <option value="0">启用</option>
                            <option value="0">停用</option>
                        </select>
                    </div>

                    <div class="layui-inline" style="padding-right: 110px;">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchUser" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>

                        <button id="btnAddUser" class="layui-btn icon-btn " ><i class="layui-icon">&#xe654;</i>优惠券核销</button>
                    </div>
                </div>
            </div>

            <table id="tableUser" lay-filter="tableUser"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="options">
    <a class="layui-btn layui-btn-primary layui-btn-xs " lay-event="">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs " lay-event="del">删除</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="store">
   {{#  if(d.Store){ }}
   {{ d.Store.name }}
   {{#  } else { }}
    商家已删除
   {{#  } }}
</script>
<script type="text/html" id="tableStateUser">
    <input type="checkbox" lay-filter="ckStateUser1" value="{{d.id}}" lay-skin="switch"
           lay-text="通过|驳回" {{d.status==1?'checked':''}}/>
</script>
<script type="text/html" id="tableStateUser1">
    <input type="checkbox" lay-filter="ckStateUser" value="{{d.id}}" lay-skin="switch"
           lay-text="启用|停用" {{d.state==1?'checked':''}}/>
</script>

<div class="layui-card-body">
    <script type="text/html" id="thumb">

        {{# if(d.thumb){ }}
        <a href="{{d.thumb}}" target="_blank" title="点击查看">
            <img src="{{d.thumb}}" alt="" width="28" height="28">
        </a>
        {{#  } }}
    </script>
</div>
<!-- js部分 -->
{include file='public/footer' /}
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;

        // 渲染表格
        var insTb = table.render({
            elem: '#tableUser',
            url: '{:__url("mall.coupon/getData")}',
            page: true,
            toolbar: true,
            cellMinWidth: 100,
            cols: [[ //表头
                {checkbox: true, fixed: true}
                , {field: 'cate', title: '优惠券种类', width: 120}
                , {field: 'content', title: '优惠券介绍', width: 120}
                , {field: 'username', title: '用户昵称' }
                , {field: 'start_time', title: '有效期开始时间'}
                , {field: 'end_time', title: '有效期结束时间'}
                , {field: 'price', title: '售价'}
                , {field: 'phone', title: '手机号'}

//                , {field: 'thumb', title: '主图', toolbar: '#thumb', width: 100}
                ,{
                    title: '状态', templet: function (d){
                        var strs = ['<span class="layui-badge layui-bg-blue">未使用</span>','<span class="layui-badge layui-bg-green">已核销</span>', '<span class="layui-badge layui-bg-red">已过期</span>'];
                        return strs[d.status];
                    }, align: 'center', width: 100
                }
//                , {field: 'create_time', title: '创建时间',width:160}

                /*, {field: 'status', title: '审核操作', toolbar: '#status', width: 100}*/
//                ,{field: 'status', sort: true,width:100, templet: '#tableStateUser', title: '状态'}
//                , {fixed: 'right', width: 140, align: 'center', title:'操作', toolbar: '#options'}
                /*, {fixed: 'right', width: 140, align: 'center', toolbar: '#options'}*/
            ]]
        });

        // 搜索
        form.on('submit(formSubSearchUser)', function (data){
            insTb.reload({where: data.field}, 'data');
        });

        // 工具条点击事件
        table.on('tool(tableUser)', function (obj) {
            var data = obj.data;

            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            }
        });

        // 显示表单弹窗
        function showEditModel(mUser) {

           admin.open({
                type: 2,
                offset:'r',
                title: '预览新闻',
                area: ['500px', '100%'],
                btnAlign: 'l',   // 按钮居中
                content: "",
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    var url ='';
                    //mUser &&  mUser.id;

                    // 回显数据
                    form.val('modelUserForm', mUser);
                    // 表单提交事件
                    form.on('submit(modelSubmitUser)', function (data) {
                        layer.load(2);
                        $.post(url, data.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 1) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({}, 'data');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                }
            });
        }
        //审核
        //
        form.on('switch(ckStateUser1)', function (obj) {
            layer.load(2);
            $.post('', {
                id: obj.elem.value,
                status: obj.elem.checked ? 1 : 0
            }, function (res) {
                layer.closeAll('loading');
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });

        //置顶
        form.on('switch(ckStateUser)', function (obj) {
            layer.load(2);
            $.post('', {
                id: obj.elem.value,
                is_top: obj.elem.checked ? 1 : 0
            }, function (res) {
                layer.closeAll('loading');
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });

    });
</script>

</body>
</html>
